package algorithm.sort;

import java.util.Arrays;

/**
 * @ Description：冒泡排序
 */
public class _02BubbleSort implements SortAlgrithm {

    public static void main(String[] args) {
        Integer[] arr = SortUtils.getRandomIntArr(10, 100);
        System.out.println(Arrays.toString(arr));
        new _02BubbleSort().sort(arr);
        System.out.println(Arrays.toString(arr));
    }

    @Override
    public <T extends Comparable<T>> T[] sort(T[] unsorted) {
        int n = unsorted.length;
        for (int i = n - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (unsorted[j].compareTo(unsorted[j + 1]) > 0) {
                    SortUtils.swap(unsorted, j, j + 1);
                }
            }
        }
        return unsorted;
    }
}
